#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"../include/vfs.h"


int exportfile(char path[], char file_path[])
{
	FILE *fp,*add;
	int i,k=0,saveblock;
	char *data,ch;
	
	FD *fd;
	Block *b;

	fd = malloc(sizeof(FD));
	strcpy(fd->path,path);

	b = malloc(sizeof(Block));

	fp = fopen(filesystem_name,"r+b");

	header=malloc(sizeof(Header));

	fread(header,sizeof(Header),1,fp);

	add = fopen(file_path,"rb+");
	
	if(add!=NULL)
		return 5;

	add = fopen(file_path,"wb+");

	if(add==NULL)
		return 2;


	data = malloc(1024*sizeof(char));

	
		fd = search(roots.naryroot,fd);

		if(fd==NULL)
		{
			//printf("\n NULL");
			return 1;
		}

		if(strcmp(path,"")==0)
			return 3;
	
		if(strcmp(file_path,"")==0)
			return 4;

		if(fd->file_type==0)
			return 6;
	
		else
		{
			saveblock=fd->block_no;

		
			for(i=0;i<header->num_blocks;i++)
			{
				fread(b,sizeof(Block),1,fp);
			
				if (b->num==saveblock)					
				{
					strcpy(data,b->buffer);
					
					break;
				}
			}

			k=strlen(data);
			i=0;
		
			//printf("\n DATA LEN:%d",k);
			//while(k)
			{
				//ch=data[i];
				fwrite(data,sizeof(char),strlen(data),add);
				//k--;
				//i++;
			}

		}

	rewind(fp);
	fwrite(header,sizeof(Header),1,fp);

	fclose(fp);
	fclose(add);

	return 0;
}








